/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import BUS.ContactBUS;
import DTO.ContactGroupDTO;
import UTIL.ConvertString;
import UTIL.SqlDataAccessHelper;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author trquanglong
 */
public class ContactGroupDAO {
    /**
     * add contact to group.
     * @param nameGroup
     * @throws java.sql.SQLException
     * @throws java.io.UnsupportedEncodingException
     */
    static public void addContactGroup(String nameGroup) throws SQLException, UnsupportedEncodingException{
        
        Connection connect = null;
        try
        {
            connect = SqlDataAccessHelper.getConnection();
            Statement stmt = (Statement) connect.createStatement();
            String utf8NameGroup = ConvertString.ConvertString2UTF8(nameGroup);
            String sqlQuery = "Insert into ContactGroups (Name) Values ('" +  utf8NameGroup + "')";
            stmt.executeQuery(sqlQuery);
            stmt.close();
        }catch (SQLException ex) {
        }
        connect.close();
    }
/**
 * get ID group by name.
 * @param nameGroup
 * @return
 * @throws java.io.UnsupportedEncodingException
 */
    public static int getIDGroup(String nameGroup) throws UnsupportedEncodingException {
        ArrayList <ContactGroupDTO> lstGroup = new ArrayList<ContactGroupDTO>();
        lstGroup = getListContactGroup();
        for (int i = 0; i < lstGroup.size(); i++)
            if (lstGroup.get(i).getNameGroup().equals(nameGroup))
                return lstGroup.get(i).getIdGroup();
        return -1;
    }
/**
 * get all of contact group.
 * @return
 * @throws java.io.UnsupportedEncodingException
 */
    static public ArrayList<ContactGroupDTO> getListContactGroup() throws UnsupportedEncodingException{
        ArrayList <ContactGroupDTO> lstGroup = new ArrayList<ContactGroupDTO>();
        Connection connect = null;
        try
        {
            connect = SqlDataAccessHelper.getConnection();
            Statement stmt = (Statement) connect.createStatement();
            String sqlQuery = "Select * from ContactGroups";
            ResultSet rs = stmt.executeQuery(sqlQuery);
            while(rs.next()){
                ContactGroupDTO group = new ContactGroupDTO();
                group.setIdGroup(rs.getInt("ID"));
                String nameGroup = rs.getString("Name");
                group.setNameGroup(ConvertString.ConvertUTF82String(nameGroup));

                lstGroup.add(group);
            }
            stmt.close();
        }catch (SQLException ex) {
        }
        return lstGroup;
    }


/**
 * delete group by name.
 * @param nameGroup
 * @throws java.io.UnsupportedEncodingException
 * @throws java.sql.SQLException
 */
    static public void deleteGroup(String nameGroup) throws UnsupportedEncodingException, SQLException{
        ContactBUS.deleteContactsOfGroup(nameGroup);
        Connection connect = null;
        try
        {
            connect = SqlDataAccessHelper.getConnection();
            Statement stmt = (Statement) connect.createStatement();
            String sqlQuery = "Delete * from ContactGroups Where Name = '" + ConvertString.ConvertString2UTF8(nameGroup) + "'";
            stmt.executeQuery(sqlQuery);
            stmt.close();
        }catch (SQLException ex) {
        }
        connect.close();

    }


}
